#include "MainFrame.h"
#include "SimpleAudioEngine.h"

USING_NS_CC;

Scene* MainFrame::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::createWithPhysics();
    //scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
    
    //gravity on/off
    //scene->getPhysicsWorld()->setGravity(Vec2(0, 0));
    
    
    auto layer = MainFrame::create();
    layer->SetPhysicEngine( scene->getPhysicsWorld());

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool MainFrame::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
    
    //debug
    auto boxObj = PhysicsBody::createEdgeBox(visibleSize, PHYSICSBODY_MATERIAL_DEFAULT);
    
    auto edgeNode = Node::create();
    edgeNode->setPosition(Point( visibleSize.width/2+origin.x, visibleSize.height/2+origin.y));
    edgeNode->setPhysicsBody(boxObj);
    
    this->addChild(edgeNode);
    
    //objects
    {
        mySprite = Sprite::create("TankFastDown.png");
        mySprite -> setPosition(Point((visibleSize.width/2) + origin.x, visibleSize.height/2 + origin.y + 200));
        
        auto mySpriteBody = PhysicsBody::createCircle(
                                                   mySprite->getContentSize().width/2,
                                                   PhysicsMaterial(0, 1, 0) );
        mySpriteBody->setCollisionBitmask(1);
        mySpriteBody->setContactTestBitmask(true);
        mySpriteBody->setDynamic(true);
        mySprite->setPhysicsBody(mySpriteBody);
        this->addChild(mySprite);
    }
    
    {
        //objects
        mySprite = Sprite::create("TankFastDown.png");
        mySprite -> setPosition(Point((visibleSize.width/2) + origin.x, visibleSize.height/2 + origin.y));
        
        auto mySpriteBody = PhysicsBody::createCircle(
                                                   mySprite->getContentSize().width/2,
                                                   PhysicsMaterial(0, 1, 0) );
        mySpriteBody->setCollisionBitmask(2);
        mySpriteBody->setContactTestBitmask(true);
        mySpriteBody->setDynamic(true);
        mySprite->setPhysicsBody(mySpriteBody);
        this->addChild(mySprite);
    }
    
    auto contactListener = EventListenerPhysicsContact::create();
    contactListener->onContactBegin = CC_CALLBACK_1(MainFrame::onContactBegin, this);
    this -> getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener, this);
    
//
    
//    this->addChild(mySprite);
//    auto action2 = MoveTo::create(3, Point(0,150));
//    mySprite->runAction(action);
//    mySprite->runAction(action2);
//    auto action3 = JumpBy::create(3, Point(100,0), 50, 3);
//    auto action4 = JumpTo::create(3, Point(100,0), 50, 3);
//    mySprite->runAction(action3);
//    ccBezierConfig bezier;
//    bezier.controlPoint_1 = Point(0,visibleSize.height/2);
//    bezier.controlPoint_2 = Point(300, -visibleSize.height/2);
//    bezier.endPosition = Point(200,100);
//    auto action = BezierBy::create(3, bezier);
//    mySprite->runAction(action);
    
    
//    auto action = Sequence::create(
//                                   RotateBy::create(1, 118),
//                                   MoveBy::create(1, Point(100,0)),
//                                   MoveBy::create(1, Point(-100,0)),NULL);
//    auto repeatAction = RepeatForever::create(action);
//    mySprite->runAction(repeatAction);
//    
//    CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("move.mp3");
//    soundInt = CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("move.mp3", true);
//    this->schedule(schedule_selector(HelloWorld::StopEffect), 5.0f);
    
    
//    auto listener = EventListenerTouchOneByOne::create();
//    listener->setSwallowTouches(true);
//    listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
//    listener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved,this);
//    listener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded,this);
    
//    auto listener = EventListenerTouchAllAtOnce::create();
//    listener->onTouchesBegan = CC_CALLBACK_2(MainFrame::onTouchesBegan, this);
////    listener->onTouchesMoved = CC_CALLBACK_2(HelloWorld::onTouchesMoved, this);
////    listener->onTouchesEnded = CC_CALLBACK_2(HelloWorld::onTouchesEnded, this);
//    
//    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
    
    return true;
}

void MainFrame::StopEffect(float dt)
{
    CocosDenshion::SimpleAudioEngine::getInstance()->stopEffect(soundInt);
}

void MainFrame::onTouchesBegan(const std::vector<cocos2d::Touch *> &touches, cocos2d::Event *event)
{
    CCLOG("onTouchesBegan");
}



bool MainFrame::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event)
{
    CCLOG("onTouchBegan x = %f, y = %f", touch->getLocation().x, touch->getLocation().y);
    return true;
}

void MainFrame::onTouchMoved(cocos2d::Touch *touch, cocos2d::Event *event)
{
    CCLOG("onTouchMoved x = %f, y = %f", touch->getLocation().x, touch->getLocation().y);
    
}

void MainFrame::onTouchEnded(cocos2d::Touch *touch, cocos2d::Event *event)
{
    CCLOG("onTouchEnded x = %f, y = %f", touch->getLocation().x, touch->getLocation().y);
}

bool MainFrame::onContactBegin(cocos2d::PhysicsContact &contact)
{
    PhysicsBody *objA = contact.getShapeA()->getBody();
    PhysicsBody *objB = contact.getShapeB()->getBody();
    
    if ( (1==objA->getCollisionBitmask() && 2 ==objB->getCollisionBitmask()) ||
        (2==objA->getCollisionBitmask() && 1 ==objB->getCollisionBitmask()))
    {
        CCLOG("Collision happen");
    }
    
    return true;
}

void MainFrame::menuCloseCallback(Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
	MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
    return;
#endif

    Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}
